home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: What the hell is THIS?!
- Date: Sat, 13 Jan 96 13:27:25 GMT
- Organization: none
- Message-ID: <821539645snz@genesis.demon.co.uk>
- References: <4d6rgh$rfu@abel.cc.sunysb.edu> <coc-1301960253420001@dal1498.computek.net>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <coc-1301960253420001@dal1498.computek.net>
- coc@computek.net "Chad Cranfill" writes:
-
- >In article <4d6rgh$rfu@abel.cc.sunysb.edu>, bmadhusu@engws12.ic.sunysb.edu
- >(Bommasamudram Madhusudan) wrote:
- >
- >> Can someone explain what
- >>
- >> int (*p)[3] is?????
- >>
- >
- >Starting with the "p", we parse the expression thusly: "p is an array of 3
- >pointers to int".
-
- No, it is a pointer to an array of 3 ints.
-
- You read a declaration from the 'inside out' i.e. you read the parts
- that are bound more closely to the identifier first. In this case the
- parentheses group the * with p so p is a pointer. OTOH int *q[3] declares an
- array of 3 pointers to int because [], and also (), bind more tightly than *.
- This is equivalent behaviour to expressions where [] and () are often said to
- have higher precedence than *.
-
- >If you need help with this (believe me, I did!) get the
- >book "Deep C Secrets".
-
- I think you'd better reread it! :-)
-
- >>
- >> I can say things like:
- >>
- >> (*p)[0] = 3; for e.g, but when I print the value using:
- >>
- >> printf("%d",(*p)[0]) I get a core dump!
-
- Unless you've initialised p to point to something, dereferencing it results
- in undefined behaviour. This is true for any type of pointer.
-
- >Instead of using the dereference operator here, you may want to just say:
- >
- >printf("%d", p[0]);
-
- No, p[0] is equivalent to *p and evaluates to an array. The value of an array
- is a pointer to its first element so this is passing an int * value to a
- function expecting an int value, i.e. undefined behaviour.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-